package com.youlu.campus.service.order;

import com.mongodb.client.result.UpdateResult;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.OrderInfo;
import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.VO.req.CheckMobileVO;
import com.youlu.campus.entity.dto.sms.SmsBaseInfoDTO;
import com.youlu.campus.entity.english.vo.request.QueryQaCommand;
import com.youlu.campus.service.cache.CacheUserService;
import com.youlu.campus.service.sms.SmsServerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class MobileBindOrderService {

    @Autowired
    private SmsServerService smsServerService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private CacheUserService cacheUserService;


    public void doBindMobileOrder(CheckMobileVO checkMobile) {
        UserPO user = checkMobile.getUserPO();
        log.info("手机号查询订单 MobileBindOrderService.doBindMobileOrder mobile->{} currentUserId ->{} start",
                checkMobile.getMobile(), user.getId());
        String mobile = checkMobile.getMobile();
        String code = checkMobile.getCode();
        if (StringUtils.isBlank(mobile)) {
            log.error("手机号查询订单 MobileBindOrderService.doBindMobileOrder mobile->{} currentUserId ->{} 手机号为空",
                    checkMobile.getMobile(), user.getId());
            throw new BusinessException("对不起，请填写手机号哦!");
        }
        if (StringUtils.isBlank(code)) {
            log.error("手机号查询订单 MobileBindOrderService.doBindMobileOrder mobile->{} currentUserId ->{} 手机号验证码为空",
                    checkMobile.getMobile(), user.getId());
            throw new BusinessException("对不起,验证码不能为空");
        }
        if (CollectionUtils.isNotEmpty(user.getMobileSet()) && user.getMobileSet().size() > 3) {
            throw new BusinessException("对不起，手机号查询订单已绑定超过3个");
        }
        SmsBaseInfoDTO dto = SmsBaseInfoDTO.builder().phone(mobile).code(code).build();
        Pair<Boolean, String> result = smsServerService.checkVerifyCode(dto);
        if (!result.getLeft()) {
            throw new BusinessException(result.getRight());
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("mobile").is(mobile));
        query.addCriteria(Criteria.where("status").nin("0", "6", "9"));
        List<OrderInfo> orderInfoList = this.mongoTemplate.find(query, OrderInfo.class);
        if (CollectionUtils.isEmpty(orderInfoList)) {
            log.warn("手机号查询订单 MobileBindOrderService.doBindMobileOrder mobile->{} currentUserId ->{} 手机号未查询到相应的订单",
                    checkMobile.getMobile(), user.getId());
            return;
        }
        log.info("手机号查询订单 MobileBindOrderService.doBindMobileOrder mobile->{} currentUserId ->{} count  ->{}",
                checkMobile.getMobile(), user.getId(), orderInfoList.size());
        cacheUserService.removeUserId(user.getId());
        query = new Query();
        query.addCriteria(Criteria.where("id").is(user.getId()));
        Update update = new Update();
        update.push("mobileSet", mobile);
        UpdateResult updateResult = this.mongoTemplate.updateFirst(query, update, UserPO.class);
        if (updateResult.getModifiedCount() > 0) {
            log.info("手机号查询订单 MobileBindOrderService.doBindMobileOrder mobile->{} currentUserId ->{} 更新绑定成功当前账户",
                    checkMobile.getMobile(), user.getId());
        }

    }
}
